window.onload = function(){
    function getId(id){
        return document.getElementById(id);
    }
    //网页头部
    function hide(id,class1,class2){
        //鼠标经过显示隐藏内容的函数
        getId(id).onmouseover = function(){
            getId(id).className += class1;
        }
        //鼠标离开隐藏内容
        getId(id).onmouseout = function(){
            getId(id).className = class2;
        }
    }
    hide("top_l"," top_lhover","top_l");//送达城市
    hide("servic"," toplist_hover","toplist");//客户服务
    hide("procurement"," toplist_hover","toplist");//企业采购
    hide("my_dd"," toplist_hover","toplist");//我的当当
    hide("search_list","search_hover","");//搜索框隐藏列表
    hide("top_hide"," top_hide_hover","item");//搜索框隐藏列表

    var city = getId("city").getElementsByTagName("a");//获取城市列表
    var hide_search = getId("hide_search").getElementsByTagName("a");//获取隐藏搜索列表项
    //切换函数————————————————————————————————
    function replace(id1,id2){
        for(let i = 0; i<id1.length; i++){
            id1[i].onclick = function(){
                var now = this.innerHTML;
                getId(id2).innerHTML = now;
            }
        }
    }
    replace(city,"now_city");//更换城市
    replace(hide_search,"search");//更换搜索框内搜索项目


//图书列表侧边栏————————————————————————————————————————————————
    var lis = getId("book_sid").getElementsByTagName("li");//获取列表项
    for(let i=0; i<lis.length ;i++){//遍历列表项
        
        lis[i].onmouseover = function(){//鼠标经过弹出
            lis[i].className = "lihover";

            var y = scrollY;//滚动条高度
            var lisT = lis[i].offsetTop;//当前列表项高度
            var lisH = lis[i].offsetHeight;
            var sid_bar = lis[i].getElementsByTagName("div")[0];//获取侧栏
            var sidH = sid_bar.offsetHeight;//侧栏的高度
            var sidT = sid_bar.offsetTop;
            sid_bar.style.top = 0 + "px"; 
            if(y>340){
                sid_bar.style.top = y - 340 + "px"; 
            }
            if(lisT+lisH>sidH+y-330){
                sid_bar.style.top = lisT + lisH - sidH + "px";
            }
            console.log(sidH+y);
        }
        lis[i].onmouseout = function(){//鼠标离开隐藏
            lis[i].className = "";
        }
    }

    //轮播图1————————————————————————————————————————————————————————
    function carousel1(){
        var timer = null;
        var index = 0;
        var t = false;

        function showBtn(){
            for(var i = 0;i<btns.length;i++){
                if(btns[i].getAttribute("class")=="span_hover"){
                    btns[i].setAttribute("class","");
                }
                btns[index].setAttribute("class","span_hover");
            }
        }

        //执行函数
        function relevant(value){
            t=true;
            var nowLeft = parseInt(getId("carousel1").style.left);
            getId("carousel1").style.left = nowLeft + value + "px";
            
            var nowLeft = parseInt(getId("carousel1").style.left);
            
            if(nowLeft>-750){
                getId("carousel1").style.left = -6000 + "px";
            }
            if(nowLeft<-6000){
                getId("carousel1").style.left = -750 + "px";
            }
            t=false;
        }

        function autoPlay(){
            timer = setInterval(function(){
                index++;
                if(index>btns.length-1){
                    index = 0;
                }
                showBtn();
                relevant(-750);
            
            },3000)
        }

        autoPlay();
        getId("carousel1_c").onmouseover = function(){
            clearInterval(timer);
        }
        getId("carousel1_c").onmouseout = function(){
            autoPlay();
        }
        //遍历所有底部按钮
        var btns = getId("buttons1").getElementsByTagName("span");
        for(let i=0; i<btns.length; i++){
            btns[i].setAttribute("input",i);
            btns[i].onmouseover = function(){
                var myIndex = this.getAttribute("input");
                var value = (myIndex-index)*(-750);
                index = this.getAttribute("input");
                console.log(value);
                relevant(value);
                showBtn();
            }
        }

    }
    carousel1();
    //——————————————————————————————————————————————————————————————————————轮播图2
    function carousel2(){
        var index = 0;
        var timer = null; 
        var animated = false;
        var btns = getId("carousel2_btns").getElementsByTagName("a");//获取到底部按钮


        //执行函数
        function relevant(value){
            animated = true;
            var time = 300;//总位移时间
            var interval = 10;//位移间隔时间
            var speed = value/(time/interval);
            var nowLeft = parseInt(getId("carousel2").style.left)+value;//当前left值
            function go(){
                if((speed<0 && parseInt(getId("carousel2").style.left)>nowLeft) || (speed>0 && parseInt(getId("carousel2").style.left)<nowLeft)){
                    
                    getId("carousel2").style.left = parseInt(getId("carousel2").style.left) + speed +"px";//变更后的left值
                    
                    setTimeout(go,interval);
                }else{
                    animated = false;

                    if(nowLeft>-750){//高于最大值执行
                        getId("carousel2").style.left = -3000 +"px";
                    }
                    if(nowLeft<-3000){//低于最小值执行
                        getId("carousel2").style.left = -750 +"px";
                    }
                }
            }
            go();
        }
    

        //按钮亮灯函数
        function showBtn(){
            for(let i=0;i<btns.length;i++){
                if(btns[i].getAttribute("class") == "ahover"){
                    btns[i].setAttribute("class","");
                }
            }
            btns[index].setAttribute("class","ahover");
        }

        //底部按钮切换
            for(let i=0; i<btns.length; i++){
                btns[i].setAttribute("index",i);
                btns[i].onmouseover = function(){
                    if(!animated){
                    var myIndex = this.getAttribute("index");
                    var value = (myIndex-index)*(-750);
                    index = this.getAttribute("index");
                    relevant(value);
                    showBtn();
                }
            }
        }
        
        getId("arouse_left2").onclick = function(){//左箭头
            if(!animated){
            index--;
            if(index<0){
                index=btns.length-1;
            }
            relevant(750);
            showBtn();
            }
        }
        getId("arouse_right2").onclick = function(){//右箭头
            if(!animated){
            index++;
            if(index>btns.length-1){
                index=0;
            }
            relevant(-750);
            showBtn();
            }
        }
    }
    carousel2();

    //轮播图3————————————————————————————————————————————————————
    function carousel3(value){
        var index = 0;
        var animated = false;
        var btns = getId("carousel3_btns").getElementsByTagName("a");

        function relevant(value){
            animated = true;
            var time = 300;
            var interval = 10;
            var speed = value/(time/interval);
            var nowLeft = parseInt(getId("carousel3").style.left) + value;

            function go(){
                if((speed<0 && parseInt(getId("carousel3").style.left)>nowLeft) || (speed>0 && parseInt(getId("carousel3").style.left)<nowLeft)){
                    getId("carousel3").style.left = parseInt(getId("carousel3").style.left) + speed + "px";
                    setTimeout(go,interval);
                }else{
                    animated = false;
                    if(nowLeft>-240){
                        getId("carousel3").style.left = -1440 + "px";
                    }
                    if(nowLeft<-1440){
                        getId("carousel3").style.left = -240 + "px";
                    }
                }
                
            }
            go();
        }

        function showBtn(){
            for(var i=0; i<btns.length; i++){
                if(btns[i].getAttribute("class") == "ahover"){
                    btns[i].setAttribute("class","")
                }
            }
            btns[index].setAttribute("class","ahover");
        }

        for(let i=0; i<btns.length; i++){
            btns[i].setAttribute("index",i);
            btns[i].onmouseover = function(){
                if(!animated){
                    var myIndex = this.getAttribute("index");
                    var value = (myIndex-index)*(-240);
                    index = myIndex;
                    relevant(value);
                    showBtn();
                }
            }
        }

        getId("arrows_left3").onclick = function(){
            if(!animated){
                index--;
                if(index<0){
                    index = btns.length-1; 
                }
                relevant(240);
                showBtn();
            }
        }
        getId("arrows_right3").onclick = function(){
            if(!animated){
                index++;
                if(index>btns.length-1){
                    index = 0; 
                }
                relevant(-240);
                showBtn();
            }
        }

        
    }
    carousel3();

    //轮播图4
    function carousel4(){
        var index = 0;
        var animated = false;
        var btns = getId("carousel4_btns").getElementsByTagName("a");

        function relevant(value){
            animated = true;
            var time = 300;
            var interval = 10;
            var speed = value/(time/interval);
            var nowLeft = parseFloat(getId("carousel4").style.left) + value;
            console.log(nowLeft);
            
            function go(){
                if((speed<0 && parseFloat(getId("carousel4").style.left)>nowLeft) || (speed>0 && parseFloat(getId("carousel4").style.left)<nowLeft)){
                    getId("carousel4").style.left = parseFloat(getId("carousel4").style.left) + speed + "px";
                    setTimeout(go,interval);
                }else{
                    animated = false;
                    if(nowLeft>-949){
                        getId("carousel4").style.left = -3800 + "px";
                    }
                    if(nowLeft<-3801){
                        getId("carousel4").style.left = -950 + "px";
                    }
                }
            }
            go();
        }
        function showBtn(){
            for(var i=0; i<btns.length; i++){
                if(btns[i].className == "ahover"){
                    btns[i].className = "";
                }
            }
            btns[index].className = "ahover";
        }
        for(let i=0; i<btns.length; i++){
            btns[i].setAttribute("index",i);
            btns[i].onmouseover = function(){
                if(!animated){
                    var value = (this.getAttribute("index") - index)*(-950);
                    index = this.getAttribute("index");
                    showBtn();
                    relevant(value);
                }
            }
        }

        getId("arrows_left4").onclick = function(){
            if(!animated){
                index--;
                if(index<0){
                    index = btns.length-1;
                }
                relevant(950);
                showBtn();
            }
        }
        getId("arrows_right4").onclick = function(){
            if(!animated){
                index++;
                if(index>btns.length-1){
                    index = 0;
                }
                relevant(-950);
                showBtn();
            }
        }
    }
    carousel4();

    //tab 效果1
    function tab1(id,class1,class2){
        var index = null;
        var btns = getId(id).getElementsByClassName("classify")[0].getElementsByTagName("a");
        var list = getId(id).getElementsByClassName("list")[0].getElementsByClassName(class1);

        function showBtn(){
            for(var i=0; i<btns.length; i++){
                if(btns[i].getAttribute("class") == class2){
                    btns[i].setAttribute("class","");
                }
                btns[index].setAttribute("class",class2);
                
                if(list[i].style.display == "block"){
                    list[i].style.display = "none";
                }
                list[index].style.display = "block";
            }
        }

        for(let i=0; i<btns.length; i++){
            btns[i].setAttribute("index",i);
            btns[i].onmouseover = function(){
                index = this.getAttribute("index");
                showBtn();
            }
        }
    }
    tab1("hot_sale","tab1_item","classifyhover");
    tab1("hot2","tab1_item","classifyhover");
    tab1("selestion","item","tab_a");
    tab1("best_buys","item","tab_a");
    
    tab1("hot_door","item","ahover");
    tab1("readers","item1","ahover");

    //tab2
    function tab2(id){
        var index = null;
        var btns = getId(id).getElementsByClassName("tab2_btn");
        var lis = getId(id).getElementsByClassName("tab2_item");
        function showBtn(){
            for(let i=0; i<btns.length; i++){
                if(lis[i].style.display == "block"){
                    lis[i].style.display = "none";
                }
            }
            lis[index].style.display = "block";
        }

        for(let i=0; i<btns.length; i++){
            btns[i].setAttribute("index",i);
            btns[i].onmouseover = function(){
                index = this.getAttribute("index");
                showBtn();
            }
        }
    }
    tab2("tab2_1");
    tab2("tab2_2");
    tab2("tab2_3");

    //战略或作伙伴
    function partner(){
        
        /*function relevant(value){
            var speed = value/43;
            console.log(getId("partner").style.top);
            function go(){
                if((speed<0 && parseInt(getId("partner").style.top)>-172) || (speed>0 && parseInt(getId("partner").style.top)<0)){
                    getId("partner").style.top = parseInt(getId("partner").style.top) + speed + "px";
                    setTimeout(go,10);
                }
            }
            go();
        }*/
        if(parseInt(getId("partner").style.top) == 0 ){
            getId("partner").style.top = parseInt(getId("partner").style.top) - 172 + "px";
            //relevant(-172);
        }else{
            getId("partner").style.top = parseInt(getId("partner").style.top) + 172 + "px";
            //relevant(172);
        }
    }
    var timer = setInterval(partner,3000);
    getId("partner").onmouseover = function(){
        clearInterval(timer);
        timer = null;
    }
    getId("partner").onmouseout = function(){
        timer = setInterval(partner,3000);
    }

    //回到顶部
    var sT;
    var time = null;
    function goTop(){
        window.onscroll = function(){
            sT = document.body.scrollTop || document.documentElement.scrollTop;
            getId("side_bar").style.top = sT + 300 + "px";
            if(sT>500){
                getId("go_top").style.display = "block";
            }else{
                getId("go_top").style.display = "none";
            }
            if(sT <= 0){
                clearInterval(time)
            }
        }
    }
    getId("go_top").onclick = function(){
        var interval = 10;
        var speed = Math.ceil(sT/interval);
        if(sT>0){
          time = setInterval(function(){
                document.body.scrollTop = document.documentElement.scrollTop = sT - speed;
            },interval)
        }
        
    }
    goTop();
}